package com.dateview.test.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ExpandableListView;

/**
 * Author: dengdayi
 * Date:2016/1/11 16:04
 * Description:
 */
public class DateView extends ExpandableListView {
    private OnGroupClickListener mOnGroupClickListener;

    public DateView(Context context) {
        super(context);
        initView();
    }

    public DateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public DateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if (mOnGroupClickListener != null) {
                    mOnGroupClickListener.onGroupClick(parent, v, groupPosition, id);
                }
                return true;//返回ture， 作用：点击不在收缩
            }
        });
    }

    /**
     * 设置是否全部展开
     *
     * @param isExpandAll
     */
    public void setIsExpandAll(boolean isExpandAll) {
        if (isExpandAll) expandAll();
    }

    public void setmOnGroupClickListener(OnGroupClickListener mOnGroupClickListener) {
        this.mOnGroupClickListener = mOnGroupClickListener;
    }

    /**
     * 全部展开
     */
    private void expandAll() {
        int count = getAdapter().getCount();
        if (count <= 0) {
            return;
        }

        for (int i = 0; i < count; i++) {
            expandGroup(i);//设置展开
        }
    }
}
